package com.cmol.peopledetectapp.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cmol.data.GlobalData;
import com.cmol.data.SettingData;
import com.cmol.peopledetectapp.MainActivity;
import com.cmol.peopledetectapp.R;

/**
 * 用户界面
 *
 * @author 张瀚文
 */
public class PersonalActivity extends AppCompatActivity {
    private TextView email;
    private ImageView imageView1, imageView2;
    private Button personButton, listButton, mapButton, settingButton, suggestButton, historyButton, exitButton;
    private View layout;
    private long exitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);

        email = findViewById(R.id.textView5);
        imageView1 = findViewById(R.id.imageView2);
        imageView2 = findViewById(R.id.imageView30);
        personButton = findViewById(R.id.PersonButton);
        listButton = findViewById(R.id.button6);
        mapButton = findViewById(R.id.MapButton);
        settingButton = findViewById(R.id.button7);
        suggestButton = findViewById(R.id.button8);
        historyButton = findViewById(R.id.button9);
        exitButton = findViewById(R.id.ExitButton1);
        layout = findViewById(R.id.PersonalBackground);

        email.setText(GlobalData.getCurrentEmail() + "");

        SystemSetting();
    }

    // 按两次返回才退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        email.setText(GlobalData.getCurrentEmail() + "");
        SystemSetting();
    }

    public void MapClick(View view) {
        startActivity(new Intent(this, MapActivity.class));
        finish();
    }

    public void ListClick(View view) {
        startActivity(new Intent(this, ListLocationActivity.class));
        finish();
    }

    public void QuitClick(View view) {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    public void SuggestClick(View view) {
        startActivity(new Intent(this, SuggestionActivity.class));
    }

    public void HistoryClick(View view) {
        startActivity(new Intent(this, HistoryActivity.class));
    }

    public void SettingClick(View view) {
        startActivity(new Intent(this, SettingActivity.class));
    }

    private void SystemSetting() {
        // 设置主题
        if (SettingData.getCOLOR() == false) {
            email.setBackgroundResource(R.drawable.background_orangeeditext);
            imageView1.setBackgroundResource(R.drawable.ic_baseline_redperson_24);
            imageView2.setBackgroundColor(0xff6d1c);
            personButton.setBackgroundResource(R.drawable.ic_baseline_redengineering_24);
            mapButton.setBackgroundResource(R.drawable.ic_baseline_redlocation_on_24);
            listButton.setBackgroundResource(R.drawable.ic_baseline_redclear_all_24);
            settingButton.setBackgroundResource(R.drawable.button_orange);
            suggestButton.setBackgroundResource(R.drawable.button_orange);
            exitButton.setBackgroundResource(R.drawable.button_red);
            historyButton.setBackgroundResource(R.drawable.button_orange);
            layout.setBackgroundResource(R.drawable.redbackground);
        } else {
            email.setBackgroundResource(R.drawable.background_editext);
            imageView1.setBackgroundResource(R.drawable.ic_person_black_24dp);
            imageView2.setBackgroundColor(0xa7e2ff);
            personButton.setBackgroundResource(R.drawable.ic_baseline_engineering_24);
            mapButton.setBackgroundResource(R.drawable.ic_place_black_24dp);
            listButton.setBackgroundResource(R.drawable.ic_clear_all_black_24dp);
            settingButton.setBackgroundResource(R.drawable.button_blue);
            suggestButton.setBackgroundResource(R.drawable.button_blue);
            exitButton.setBackgroundResource(R.drawable.button_red);
            historyButton.setBackgroundResource(R.drawable.button_blue);
            layout.setBackgroundResource(R.drawable.bluebackground);
        }

        // 设置语言
        if (SettingData.getLANGUAGE() == false) {
            settingButton.setText("System Setting");
            suggestButton.setText("Advice Feedback");
            historyButton.setText("History");
            exitButton.setText("Log Out");

        } else {
            settingButton.setText("系统设置");
            suggestButton.setText("建议反馈");
            historyButton.setText("历史记录");
            exitButton.setText("退出登录");
        }
    }
}
